import axios, {
  AxiosInstance,
  AxiosError,
  AxiosResponse,
  AxiosRequestConfig,
} from "axios";
import router from "../router";

const service: AxiosInstance = axios.create({
  // baseURL: "http://192.168.2.111:8445/hxt-bms",
  // baseURL: "https://a725827c01.yicp.fun/hxt-bms",
  baseURL: "https://bms.hxtetc.com/hxt-bms/",
  timeout: 10000,
});

service.interceptors.request.use(
  (config: AxiosRequestConfig) => {
    const token = window.localStorage.getItem("X-Access-Token");
    if (token) {
      config.headers["token"] = token;
    }
    return config;
  },
  (error: AxiosError) => {
    return Promise.reject();
  }
);

service.interceptors.response.use(
  (response: AxiosResponse) => {
    return response;
  },
  (error: AxiosError) => {
    if (error.response.data.code == 5002 || error.response.data.code == 5003) {
      router.push("/login");
    }
    return error;
  }
);

export default service;
